
var keystack = [];
var is_digraph = 0;

var textbox = docRef.editform.text_data;
var digraphs = 
{
     "a'" : "á",  "A'" : "À",
     "a!" : "à",  "A!" : "À",
     "a>" : "â",  "A>" : "Â",
     "a:" : "ä",  "A:" : "Ä",
     "a-" : "ā",  "A-" : "Ā",
     "a(" : "ă",  "A(" : "Ă",
     "aa" : "å",  "AA" : "Å",
     "a?" : "ã",  "A?" : "Ã",
     "a3" : "ǣ",  "A3" : "Ǣ",

     "A+" : "א",
     "B+" : "ב", "G+" : "ג", "D+" : "ד",
     "H+" : "ה", "W+" : "ו", "X+" : "ח",
     "Tj" : "ט", "J+" : "י", "K%" : "ך",
     "K+" : "כ", "L+" : "ל", "M%" : "ם",
     "M+" : "מ", "N%" : "ן", "N+" : "נ",
     "S+" : "ס", "E+" : "ע", "P%" : "ף",
     "P+" : "פ", "Zj" : "ץ", "ZJ" : "צ",
     "Q+" : "ק", "R+" : "ר", "Sh" : "ש",
     "T+" : "ת",
                              
     "e'" : "é",  "E'" : "É",
     "e!" : "è",  "E!" : "È",
     "e>" : "ê",  "E>" : "Ê",
     "e:" : "ë",  "E:" : "Ë",
     "e-" : "ē",  "E-" : "Ē",
     "e(" : "ĕ",  "E(" : "Ĕ",
     "e?" : "ẽ",  "E?" : "Ẽ",
                              
     "i'" : "í",  "I'" : "Í",
     "i!" : "ì",  "I!" : "Ì",
     "i>" : "î",  "I>" : "Î",
     "i:" : "ï",  "I:" : "Ï",
     "i-" : "ī",  "I-" : "Ī",
     "i(" : "ĭ",  "I(" : "Ĭ",
     "i?" : "ĩ",  "I?" : "Ĩ",
                              
     "o'" : "ó",  "O'" : "Ó",
     "o!" : "ò",  "O!" : "Ò",
     "o>" : "ô",  "O>" : "Ô",
     "o:" : "ö",  "O:" : "Ö",
     "o-" : "ō",  "O-" : "Ō",
     "o(" : "ŏ",  "O(" : "Ŏ",
     "o?" : "õ",  "O?" : "Õ",
     "o/" : "ø",  "O/" : "Ø",
                              
     "u'" : "ú",  "U'" : "Ú",
     "u!" : "ù",  "U`" : "Ù",
     "u>" : "û",  "U>" : "Û",
     "u:" : "ü",  "U:" : "Ü",
     "u-" : "ū",  "U-" : "Ū",
     "u(" : "ŭ",  "U(" : "Ŭ",
     "u?" : "ũ",  "U?" : "Ũ",

     "y'" : "ý",  "Y'" : "Ý",
     "y!" : "ỳ",  "Y`" : "Ỳ",
     "y>" : "ŷ",  "Y>" : "Ŷ",
     "y:" : "ÿ",  "Y:" : "Ÿ",

     "-o" : "º",  "-a" : "ª",
     "c," : "ç",  "C," : "Ç",
     "n?" : "ñ",  "N?" : "Ñ",
     "SE" : "§",  "DO" : "$",
     "ae" : "æ", "AE" : "Æ",
     "oe" : "œ", "OE" : "Œ",

     "'\"" : "˝", "+=" : "±",
     "''" : "´",  "'!" : "`",
     "d-" : "ð",  "D-" : "Ð",
     "th" : "þ",  "TH" : "Þ",

     ".M" : "·",  "*X" : "×",
     ":-" : "÷", 

     "a*" : "α",  "A*" : "Α",
     "a%" : "ά",  "A%" : "Ά",
     ";!" : "ἂ",  ",!" : "ἃ",
     "?;" : "ἄ",  "?," : "ἅ",
     "!:" : "ἆ",  "?:" : "ἇ", 
     "b*" : "β",  "B*" : "Β",
     "c*" : "ξ",  "C*" : "Ξ",
     "d*" : "δ",  "D*" : "Δ",
     "e%" : "έ",  "E%" : "Έ",
     "f*" : "φ",  "F*" : "Φ",
     "g*" : "γ",  "G*" : "Γ",
     "m3" : "ϝ",  "M3" : "Ϝ",
     "h*" : "θ",  "H*" : "Θ",
     "i*" : "ι",  "I*" : "Ι",
     "i%" : "ί",  "I%" : "Ί",
     "i3" : "ΐ",  "u3" : "ΰ",  
     "j*" : "ϊ",  "J*" : "Ϊ",
     "j3" : "ϵ",  "J3" : "3",
     "k*" : "κ",  "K*" : "Κ",
     "k3" : "ϟ",  "K3" : "Ϟ", 
     "l*" : "λ",  "L*" : "Λ",
     "m*" : "μ",  "M*" : "Μ",
     "n*" : "ν",  "N*" : "Ν",
     "o*" : "ο",  "O*" : "Ο",
     "o%" : "ό",  "O%" : "Ό",
     "p*" : "π",  "P*" : "Π",
     "p3" : "ϡ",  "P3" : "Ϡ",
     "q*" : "ψ",  "Q*" : "Ψ",
     "r*" : "ρ",  "R*" : "Ρ",
     "s*" : "σ",  "S*" : "Σ",
     "*s" : "ς",  "*S" : "Σ",
     "t*" : "τ",  "T*" : "Τ",
     "t3" : "ϛ",  "T3" : "Ϛ",
     "u*" : "υ",  "U*" : "Υ",
     "u%" : "ύ",  "U%" : "Ύ",
     "v*" : "ϋ",  "V*" : "Ϋ",
     "w*" : "ω",  "W*" : "Ω",
     "w%" : "ώ",  "W%" : "Ώ",
     "x*" : "χ",  "X*" : "Χ",
     "y*" : "η",  "Y*" : "Η",
     "y%" : "ή",  "Y%" : "Ύ",
     "z*" : "ζ",  "Z*" : "Ζ",
     "g," : "ģ",  "G," : "Ģ",

     "<<" : "«", ">>" : "»",

     "0S" : "⁰", "1S" : "¹",
     "2S" : "²", "3S" : "³", "4S" : "⁴", "5S" : "⁵",
     "6S" : "⁶", "7S" : "⁷", "8S" : "⁸", "9S" : "⁹",
     "12" : "½", "14" : "¼", "34" : "¾",
     "18" : "⅛", "38" : "⅜", "58" : "⅝", "78" : "⅞",
     "13" : "⅓", "23" : "⅔", "16" : "⅙", "56" : "⅚",
     "15" : "⅕", "25" : "⅖", "35" : "⅗", "45" : "⅘", 

     '"6' : "“", '"9' : "”", "'6" : "‘", "'9" : "’",
     "DG" : "°", 
};

function digraph(c)
{
    if(digraphs[c])
        return digraphs[c];
    else
        return null;
}

// using keyUp since some browsers don't trap backspace
// (Could use keyUp for backspace only ...)

// note weird event ownership due to frames.
function eKeyUp(e)
{
    if(! e) 
        var e = this.document.parentWindow.event;

    var kCode = (e.which && typeof e.which == "number")
        ? e.which
        : e.keyCode;

    if(kCode != 8)
        return true;

    // backspace!
    switch(keystack.length)
    {
        case 0: // bkspc for 1st character - ignore
            break;

        case 1: // cocking digraph, push 8 as flag
            keystack.push(8);
            // then pass bkspc 
            break;

        default: // second bkspc - uncock digraph 
            keystack.pop();
            break;
    }
    return true;
}

function eKeySpy(e)
{
    if(! e) 
        var e = this.document.parentWindow.event;
    var kCode = (e.which && typeof e.which == "number")
        ? e.which
        : e.keyCode;
    var kChar = e.charCode;

    switch(kCode)
    {
        case 8:
            // defer to keyUp for backspace trapping

            /*
            switch(keystack.length)
            {
                case 0: // bkspc for 1st character - ignore
                    return true;
                case 1: // cocking digraph, push 8 as flag
                    keystack.push(kCode);
                    // then pass bkspc 
                    return true;
                default: // second bkspc - uncock digraph 
                    keystack.pop();
                    return true;
            }
            */
        case 16: // IE passes the shift key, which is needed
                 // for shifted digraphs, so ignore it like others
            return true;
        default:
            switch(keystack.length)
            {
                case 1:
                    keystack.pop(); // pop bkspc and drop through
                case 0:
                    keystack.push(kCode);
                    return true;
                default:    // digraph cocked
                    keystack.pop(); // pop bkspc flag char
                    break;
            }
    }

    // should only get here if collecting char 2
    // time to fire digraph
    // get the stored char
    var char1 = String.fromCharCode(keystack.pop());
    var char2 = String.fromCharCode(kCode);
    var key = char1 + char2;
    var val = textbox.value;

    var mappedChar = digraph(key);

    if(! mappedChar)
    {
        return true;
    }

    if (typeof textbox.selectionStart == "number" 
                 && typeof textbox.selectionEnd == "number") 
    {
        // Non-IE browsers and IE 9
        var itop   = textbox.scrollTop ;
        var start = textbox.selectionStart;
        var end = textbox.selectionEnd;
        textbox.value = val.slice(0, start) 
                        + mappedChar + val.slice(end);

        // Move the caret
        this.selectionStart = this.selectionEnd = start + 1;
        textbox.scrollTop = itop;
    }
    else if (docRef.selection && docRef.selection.createRange) 
    {
        // For IE up to version 8
        var selectionRange = docRef.selection.createRange();
        var textInputRange = docRef.createTextRange();
        var precedingRange = docRef.createTextRange();
        var bookmark = selectionRange.getBookmark();
        textInputRange.moveToBookmark(bookmark);
        precedingRange.setEndPoint("EndToStart", textInputRange);
        start = precedingRange.text.length;
        end = start + selectionRange.text.length;
        docRef.value = val.slice(0, start) 
                                + mappedChar + val.slice(end);
        start++;

        // Move the caret
        textInputRange = docRef.createTextRange();
        textInputRange.collapse(true);
        textInputRange.move("character", 
            start 
            - (docRef.value.slice(0, start).split("\r\n").length - 1));
        textInputRange.select();
    }

    return false;
}

function Explain()
{
    var d = NewBlankWindow().document;
        d.write( "<pre style='font-family: \"Courier New\"'>"
            + docRef.value
                .replace(/&/g, '&amp;')
                .replace(/</g, '&lt;')
                .replace(/>/g, '&gt;')
                .replace(/&lt;(\/?)(i|b|hr)&gt;/ig, '<$1$2>')
                .replace(/&lt;sc&gt;/ig, 
                    '<span style="font-variant: small-caps;">')
                .replace(/&lt;\/sc&gt;/ig, '</span>')
            + "</pre>" );
    d.close();
    return false;
}
